package ueb15;

import de.htw.saarland.stl.Stdin;
import java.io.IOException;

public class Dialog {

	private StringBuffer 		dLine 	= new StringBuffer();
 	private RedBlackTree		manager = new RedBlackTree();

	// Objektinitialisierungsblock
	{
		// Erstellen der Trennzeile
		for(int i = 0; i <= Properties.LENGTH; i++) {
			dLine.append(Properties.DCHAR);
		}
	}

	public void process(String command) {
		String fileName = null;

		fileName = command.split(Properties.REGEX)[1];
		add(fileName);
	}

	public void add(String fileName) {
		StringLineReader reader = null;
		String 	line 		= null;
		boolean eof 		= false;
		int 	counter		= 0;
		String	output		= null;

		try {
			reader = new StringLineReader(fileName);

			while(!eof) {
				try {
					line = reader.read(counter);
				} catch(IOException io) {
					eof = true;
					counter = 0;
				}

				// zeile zum tree hinzufügenund ausgabe des status
				if(line != null) {
					manager.insertValue(line);
					System.out.println(output);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
				counter = 0;
			} catch(IOException io) {
				io.printStackTrace();
			}
		}
                manager.toString();
	}

	public void run() {
		String command 	= null;
		boolean end	= false;

		while(!end) {
			System.out.println(divLine());
			command = Stdin.readString(Properties.MESSAGE);
			System.out.println(divLine());

			if(command.equalsIgnoreCase(Properties.END)) {
				end = true;
			} else {
				try {
					process(command);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
			}
		}
		System.out.println(Properties.ENDMESSAGE);
	}

	public String divLine() {
		return dLine.toString();
	}

	public static void main(String[] args) {
		Dialog dialog = new Dialog();
		dialog.run();
	}
}
